﻿using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

    // 速度
    public float m_speed = 1;
    // 旋转速度
    protected float m_rotSpeed = 30;
    // 变向间隔时间
    protected float m_timer = 1.5f;
    protected Transform m_transform;
    // 生命
    public float m_life = 10;

    // 声音
    public AudioClip m_shootClip;

    // 声音源
    protected AudioSource m_audio;

    // 爆炸特效
    public Transform m_explosionFX;

    // 得分
    public int m_point = 10;

	// Use this for initialization
	void Start () {
        m_transform = this.transform;
        m_audio = this.GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
        UpdateMove();
	}

    protected virtual void UpdateMove()
    {

        m_timer -= Time.deltaTime;
        if (m_timer <= 0)
        {
            m_timer = 3;
            //改变旋转方向
            m_rotSpeed = -m_rotSpeed;
        
        }
        // 旋转方向
        m_transform.Rotate(Vector3.up,m_rotSpeed*Time.deltaTime,Space.World);

        //前进
        m_transform.Translate(new Vector3(0,0,-m_speed*Time.deltaTime));
    }


    //碰触函数
    void OnTriggerEnter(Collider other)
    {

        if (other.tag.CompareTo("PlayerRocket") == 0)
        {

            Rocket rocket = other.GetComponent<Rocket>();
            if (rocket != null)
            {

                m_life -= rocket.m_power;

                if (m_life <= 0)
                {
                    //毁灭得分
                    GameManager.Instance.AddScore(m_point);
                    //爆炸特效
                    Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
                    //消除物体
                    Destroy(this.gameObject);
                }
            }
        }
        else if (other.tag.CompareTo("Player") == 0)
        {
            m_life = 0;
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
            Destroy(this.gameObject);
        }

        if (other.tag.CompareTo("bound") == 0)
        {
            Instantiate(m_explosionFX, m_transform.position, Quaternion.identity);
            m_life = 0;
            Destroy(this.gameObject);
        }
    }


}
